package com.hitplane.core 
{
	import com.hitplane.debug.DebugTrace;
	import com.hitplane.elements.state.FlyDealState;
	import com.hitplane.elements.state.FlyHitState;
	import com.hitplane.elements.state.FlyNormalState;
	import com.hitplane.interfaces.IAttack;
	import com.hitplane.interfaces.IFly;
	import com.hitplane.interfaces.IState;
	
	import flash.events.Event;

	/**
	 * @describe  	飞行器
	 * @author  	rrDog
	 * @website 	http://blog.sina.com.cn/zihua2007
	 * @time		... 
	 */
	public class Fly extends ItemBaseDisplay implements IFly
	{
		/**
		 * 攻击方式
		 */
		private var _attackArr:Array;
		
		protected var speed:int;
		protected var blood:int=0;
		
		public var flyNormalState:IState;
		public var flyHitState:IState;
		public var flyDealState:IState;
		//private====================================================================
		/**
		 * hit动画执行完毕
		 */
		private function onHitAnimalEnd(e:Event):void 
		{
			setState(flyNormalState);
		}
		private function onDealAnimalEnd(e:Event):void 
		{
			dispose();
		}
		//override===============================================================
		override protected function addEvent():void{
			this.addEventListener(FlyHitState(flyHitState).ANIMAL_END, onHitAnimalEnd);
			this.addEventListener(FlyDealState(flyDealState).ANIMAL_END, onDealAnimalEnd);
		}
		override protected function removeEvent():void{
			this.removeEventListener(FlyHitState(flyHitState).ANIMAL_END, onHitAnimalEnd);
			this.removeEventListener(FlyDealState(flyDealState).ANIMAL_END, onDealAnimalEnd);
		}
		override protected function initState():void{
			flyNormalState = new FlyNormalState(this);
			flyHitState = new FlyHitState(this);
			flyDealState = new FlyDealState(this);
		}
		override public function hasCollision():void{
			setState(flyHitState);
			blood--;
			if (blood <= 0) {
				setState(flyDealState);
				removeFromManager();
			}
		}
		//public======================================================================
		
		/* INTERFACE com.hitplane.interfaces.IFly */
		public function registerAttack(attackPattern:String,attack:IAttack):void {
			if (_attackArr == null) {
				_attackArr = new Array();
			}
			_attackArr[attackPattern] = _attackArr[attackPattern] || attack;
		}
		
		public function attack(attackPattern:String):void {
			if (_attackArr != null &&isInStage) {
				if( _attackArr[attackPattern] != null){
					(_attackArr[attackPattern] as IAttack).action();
				}
			}else{
				DebugTrace.traceObj(this,"不存在的攻击方式，或者攻击载体没有在舞台。");
			}
		}
		
	}
	
}